package net.adetech.cjg.innerservice.utils;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SixInOneUtils {
    @Value("${6in1.url}")
    private String baseUrl;


    private static PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    static{
        cm.setMaxTotal(300);
        cm.setDefaultMaxPerRoute(20);
    }
    private static CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();


    public JSONObject write(JSONObject inputJson,String xtlb,String jkid)
    {
        try {
            inputJson.put("ixtlb",xtlb);
            inputJson.put("ijkid",jkid);
            StringEntity entity = new StringEntity(inputJson.toJSONString(),"UTF-8");
            entity.setContentType("application/json");
            HttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(baseUrl+"/tmriws/write");
            post.setEntity(entity);


            HttpResponse httpResponse = client.execute(post);
            String responseString = EntityUtils.toString(httpResponse.getEntity());
            JSONObject resultJson = JSONObject.parseObject(responseString);
            return resultJson;
        }catch (Exception e)
        {
            e.printStackTrace();
            return new JSONObject();
        }
    }


    public JSONObject call(JSONObject inputJson,String jkid)
    {
        try {
            StringEntity entity = new StringEntity(inputJson.toJSONString(),"UTF-8");
            entity.setContentType("application/json");
            //HttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(baseUrl+"/tmriws/"+jkid);
            post.setEntity(entity);
            post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

            HttpResponse httpResponse = httpClient.execute(post);
            String responseString = EntityUtils.toString(httpResponse.getEntity());
            JSONObject resultJson = JSONObject.parseObject(responseString);
            return resultJson;
        }catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }



//    /**
//     * 医院体检信息查询接口
//     * @param sfzmhm 身份证明号码
//     * @return sfytjxx 是否有体检信息  1是 2否
//     */
//    public JSONObject c0290(String sfzmhm)
//    {
//        try {
//            JSONObject paramJson = new JSONObject();
//            paramJson.put("sfzmhm",sfzmhm);
//            StringEntity entity = new StringEntity(paramJson.toJSONString(),"UTF-8");
//            entity.setContentType("application/json");
//            HttpClient client = HttpClients.createDefault();
//            HttpPost post = new HttpPost(baseUrl+"/tmriws/02C90");
//            post.setEntity(entity);
//            HttpResponse httpResponse = client.execute(post);
//            String responseString = EntityUtils.toString(httpResponse.getEntity());
//            JSONObject resultJson = JSONObject.parseObject(responseString);
//            resultJson.put("result","ok");
//            return resultJson;
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//            JSONObject resultJson = new JSONObject();
//            resultJson.put("result","error");
//            return  resultJson;
//        }
//    }
//
//
//    /**
//     *
//     * @param inputJson
//     * @return
//     */
//    public JSONObject c0289(JSONObject inputJson)
//    {
//        try {
//            StringEntity entity = new StringEntity(inputJson.toJSONString(),"UTF-8");
//            entity.setContentType("application/json");
//            HttpClient client = HttpClients.createDefault();
//            HttpPost post = new HttpPost(baseUrl+"/tmriws/02C89");
//            post.setEntity(entity);
//            HttpResponse httpResponse = client.execute(post);
//            String responseString = EntityUtils.toString(httpResponse.getEntity());
//            JSONObject resultJson = JSONObject.parseObject(responseString);
//            resultJson.put("result","ok");
//            return resultJson;
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//            JSONObject resultJson = new JSONObject();
//            resultJson.put("result","error");
//            return  resultJson;
//        }
//    }


}
